package com.example.gkcircle.library;

import android.graphics.Bitmap;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.example.gkcircle.App;
import com.example.gkcircle.library.object.URL;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jehu on 16/3/22.
 */
public class HttpRequest {


    private final String TAG="HttpRequest";
    private static HttpRequest mHttpRequest;

    public static HttpRequest getIntance(){
        if(mHttpRequest ==null){
            mHttpRequest =new HttpRequest();
        }
        return mHttpRequest;
    }


    //get请求String
    public IGetStringListener igsl;
    public interface IGetStringListener{
        void succeed(String result);
        void error(String error);
    }

    public void getRequestString(final String url,final int type,final IGetStringListener igsl){
        this.igsl=igsl;
        com.android.volley.toolbox.StringRequest request=new com.android.volley.toolbox.StringRequest(com.android.volley.Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                igsl.succeed(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                igsl.error(error.toString());
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type","text/html; charset=utf-8");
                if(App.getCookie(type)!=null){
                    headers.put("Cookie",App.getCookie(type));
                }
                return headers;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {

                Map<String, String> responseHeaders = response.headers;
                String cookie = responseHeaders.get("Set-Cookie");
                Log.i(TAG, "getString获取的cookie: "+cookie);
                App.setCookie(type,cookie);
                String str = null;
                try {
                    str = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(str,
                        HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        App.getmHttpQueue().add(request);
    }







    //获取图片验证码
    public IImageRequest ilr;
    public interface IImageRequest{
        public void complete(Bitmap bitmap);
    };

    public void getCaptchaBitmap(final IImageRequest ilr){
        this.ilr=ilr;
        ImageRequest request=new ImageRequest(URL.captcha, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                ilr.complete(response);
            }
        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ilr.complete(null);
            }
        }){

            @Override
            protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
                Map<String, String> responseHeaders = response.headers;
                String cookie = responseHeaders.get("Set-Cookie");
                Log.i(TAG, "获取cookie为：  "+cookie);
                App.setCookie(1,cookie);
                return super.parseNetworkResponse(response);
            }
        };
        App.getmHttpQueue().add(request);
    }

}
